package ink.lovejinhu.swardToOffer;

import ink.lovejinhu.common.ListNode;

/**
 * @author jinhu
 * created 2021-08-18 9:43
 */
public class Offer_025 {
    /**
     * 合并两个有序的链表
     *
     * @param l1
     * @param l2
     * @return
     */
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode new_head = new ListNode();
        ListNode res = new_head;
        while (l1 != null && l2 != null) {
            if (l1.val > l2.val) {
                new_head.next = l2;
                new_head = new_head.next;
                l2 = l2.next;
            } else {
                new_head.next = l1;
                new_head = new_head.next;
                l1 = l1.next;
            }
        }
        if (l1 == null) {
            new_head.next = l2;
        }
        if (l2 == null) {
            new_head.next = l1;
        }
        return res.next;
    }
}
